<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/4/20
 * Time: 下午9:11
 */
namespace Admin\Controller;

use Common\Controller\AdminBaseController;

class ArtController extends AdminBaseController{
    public function index(){
        $art = M('art_cate');
        $list = $art->field('id,title,pid,sort,keyword,description,status')->select();
        $list = getTree($list,0,0,'|----');
        $this->assign('list',$list);
        $this->display();
    }

    public function changeDel(){
        if(IS_POST){
            $id = I('post.id/d',0);
            if(empty($id)){
                $this->ajaxReturn(['status' => 1,'msg' => '非法操作']);
            }
            $find = M('art_cate')->where(['id' => $id])->delete();
            if($find){
                $this->ajaxReturn(['status' => 0,'msg' => '删除成功']);
            }else{
                $this->ajaxReturn(['status' => 1,'msg' => '删除失败']);
            }
        }else{
            $this->ajaxReturn(['status' => 1,'msg' => '非法操作']);
        }
    }

    public function save(){
        if(IS_POST || IS_AJAX){
            $id = I('post.id/d',0);
            $artname = I('post.artname/s','');
            if(!$artname){
                $this->ajaxReturn(['status' => 1,'msg' => '栏目名称不能为空']);
            }
            $pid = I('post.pid/d',0);
            $sort = I('post.sort/d',0);
            $keyword = I('post.keyword/s','');
            if(!$keyword){
                $this->ajaxReturn(['status' => 1,'msg' => '关键词不能为空']);
            }
            $description = I('post.description/s','');
            if(!$description){
                $this->ajaxReturn(['status' => 1,'msg' => '描述不能为空']);
            }
            $status = I('post.status/d',0);
            $art = M('art_cate');
            $admindata = $art->where(['title' => $artname])->find();
            if($admindata && empty($id)){
                $this->ajaxReturn(['status' => 1,'msg' => '栏目已存在']);
            }
            $data = [
                'id' => $id,
                'title' => $artname,
                'pid' => $pid,
                'sort' => $sort,
                'keyword' => $keyword,
                'description' => $description,
                'status' => $status
            ];
            $isart = $art->add($data,[],true);
            if(!$isart){
                $this->ajaxReturn(['status' => 1,'msg' => '失败']);
            }
            $this->ajaxReturn(['status' => 0,'msg' => '成功']);
        }else{
            $this->ajaxReturn(['status' => 1,'msg' => '非法操作']);
        }
    }

    public function edit(){
        if(IS_GET){
            $id = I('get.id/d',0);
            if(!$id){
                $this->error('非法操作');
            }
            $art = M('art_cate')->field('id,title,pid,sort,keyword,description,status')->where(['id' => $id])->find();
        }
        $this->assign('art',$art);
        $this->display();
    }

    public function add(){
        if(IS_GET){
            $id = I('get.id/d',0);
            if(empty($id)){
                $pid = I('get.pid/d',0);
                $list = ['pid' => $pid];
                $this->assign('list',$list);
            }else{
                $art = M('art_cate')->field('id,title,pid,sort,keyword,description,status')->where(['id' => $id])->find();
                $this->assign('art',$art);
            }
            $this->display();
        }
    }

    public function changeMode(){
        if(IS_POST){
            $id = I('post.id/d',0);
            if(empty($id)){
                $this->ajaxReturn(['status' => 1,'msg'=>'非法操作']);
            }
            $art = M('art_cate');
            $find = $art->field('status')->where(['id' => $id])->find();
            if(empty($find['status'])){
                $art->where(['id' => $id])->setField('status','1');
                $this->ajaxReturn(['status' => 0,'msg'=>'已开启','data' => 1]);
            }else{
                $art->where(['id' => $id])->setField('status','0');
                $this->ajaxReturn(['status' => 0,'msg'=>'已开启','data' => 0]);
            }
        }else{
            $this->ajaxReturn(['status' => 1,'msg'=>'非法操作']);
        }
    }
}